package com.zhengdongzulin.zuke.app.main.apartment

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.lxj.androidktx.core.*

import com.xuexiang.xui.widget.banner.widget.banner.BannerItem
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.ConfigInfo
import com.zhengdongzulin.zuke.app.data.HouseTypeDetaisl
import com.zhengdongzulin.zuke.app.main.ZKWebviewActivity
import com.zhengdongzulin.zuke.app.main.apartment.sign.ZKReserveSearchActivity
import com.zhengdongzulin.zuke.app.main.mine.ZKIdAuthActivity
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityHouseDetailsBinding
import com.zhengdongzulin.zuke.ext.*


/**
 * @类名称: CLASS
 * @类描述: 房型详情
 * @创建人：Jeff
 * @创建时间：2021/6/9 20:59
 * @备注：
 */
class ZKHouseDetailsActivity : BaseActivity<HouseDetailsVm, ZkActivityHouseDetailsBinding>() {

    private var houseItemName = ""
    lateinit var mData: HouseTypeDetaisl

    override fun getLayoutResId(): Int = R.layout.zk_activity_house_details

    override fun initVM(): HouseDetailsVm = getVM(HouseDetailsVm::class.java)

    override fun initView() {
    }

    override fun initData() {
        mViewModel.room_type_detail(intent.getStringExtra("id"))
        if (!TextUtils.isEmpty(intent.getStringExtra("houseItemName"))) {
            houseItemName = intent.getStringExtra("houseItemName")!!
        }

    }

    @Suppress("DEPRECATION")
    override fun initListener() {
        mView.collection.setOnClickListener {
            if (checkLogin()) {
                if (mData.collectionStatus == 0) { //0 未收藏 1 收藏
                    mData.collectionStatus = 1
                    mViewModel.collection_add(mData.id)
                    mView.collection.setImageResource(R.mipmap.icon_details_collection_sel)
                } else {
                    mData.collectionStatus = 0
                    mView.collection.setImageResource(R.mipmap.icon_details_collection)
                    mViewModel.collection_cancel(mData.id)
                }
            }
        }

        mView.ivVr.setOnClickListener {
            mData.vrUrl?.let {
                Intent().put("url", mData.vrUrl).start(this, ZKWebviewActivity::class.java)
            }
        }

    }

    override fun startObserve() {
        mViewModel.room_type_detail.observe(this, Observer {
            it.let {
                mData = it
                var imagesData = mutableListOf<BannerItem>()
                if (!it.picList.isNullOrEmpty()) {
                    for (roomPic in it.picList) {
                        imagesData.add(BannerItem().setImgUrl(roomPic.url))
                    }
                    mView.tvCount.text = "1/${imagesData.size}"
                    mView.banner.setSource(imagesData)?.startScroll()
                    mView.banner.viewPager.addOnPageChangeListener(object :
                        ViewPager.OnPageChangeListener {
                        override fun onPageScrollStateChanged(state: Int) {

                        }

                        override fun onPageScrolled(
                            position: Int,
                            positionOffset: Float,
                            positionOffsetPixels: Int
                        ) {

                        }

                        override fun onPageSelected(position: Int) {
                            mView.tvCount.text = "${position + 1}/${imagesData.size}"
                        }
                    })
                }
                if (it.collectionStatus == 0) {// 0 未收藏 1 收藏
                    mView.collection.setImageResource(R.mipmap.icon_details_collection)
                } else {
                    mView.collection.setImageResource(R.mipmap.icon_details_collection_sel)
                }
                mView.titleName.text = it.roomTypeName
                if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                    mView.collection.gone()
                    mView.money.text = ""
                    mView.money.appendStyleSpan("面议")
                    mView.ivReserve2.setImageResource(R.mipmap.room_details_icon_en)
                    mView.ivReserve.setImageResource(R.mipmap.room_details_icon_en2)
                    mView.tvReserve.setColor(R.color.gray_ca)
                    mView.tvReserve2.setColor(R.color.gray_ca)
                    mView.sign.setBackgroundResource(R.drawable.apartment_details_bottom_button_bg_en)
                    mView.call.setBackgroundResource(R.drawable.apartment_details_bottom_button_bg_en)
                } else {
                    mView.collection.visible()
                    mView.money.text = "￥${it.houseMinMoney}"
                    mView.money.appendSizeSpan("/月", 0.46f)
                        .colorSpan(
                            "",
                            mView.money.text.length - 2..mView.money.text.length,
                            color(R.color.black_66)
                        )
                        .styleSpan("", 0..mView.money.text.length - 2)
                    mView.llEmpty.setOnClickListener {
                        if (checkLogin()) {
                            mShareViewModel.houseDetails.value = mData!!
                            Intent().put("houseItemName", houseItemName)
                                .put("roomTypeName", mData.roomTypeName)
                                .put("phone", mData.managerInfo.managerPhone)
                                .put("houseId", mData.id).put("yuDing", true).put(
                                    "url",
                                    if (mData.picList != null && mData.picList.size > 0) mData.picList[0].url else ""
                                ).start(this, ZKReserveSearchActivity::class.java)
                        }
                    }
                    mView.llEmpty2.setOnClickListener {
                        if (checkLogin()) {
                            mShareViewModel.houseDetails.value = mData!!
                            Intent().put("type", "houseDetails")
                                .start(this@ZKHouseDetailsActivity, ZKReserveActivity::class.java)
                        }
                    }
                    mView.sign.setOnClickListener {
                        if (checkLogin()) {
                            mViewModel.check_sign_online(mData.id)
                        }
                    }
                    mView.call.setOnClickListener {
                        if (checkLogin()) {
                            callPhoneDialog(
                                mData.managerInfo.managerName,
                                mData.managerInfo.managerPhone
                            ).show()
                        }
                    }
                }
                mView.typeCount.text =
                    "${it.doorName}\u0020\u0020${it.area}㎡\u0020\u0020${it.orientation}"
                mView.addressDesc.text = it.address
                if (it.introduction.isNullOrEmpty()) {
                    mView.introduceDesc.text = "暂无描述"
                } else {
                    mView.introduceDesc.text = it.introduction
                }
                mView.introduceMore.setOnClickListener {
                    var v = View.inflate(this, R.layout.popwindow_house_introduce_more, null)
                    var dialog = getBottomDialog(v, 0.4)
                    v.findViewById<View>(R.id.iv_close).setOnClickListener { dialog.dismiss() }
                    v.findViewById<View>(R.id.tv_close).setOnClickListener { dialog.dismiss() }
                    v.findViewById<TextView>(R.id.project_desc).text =
                        if (mData.introduction.isNullOrEmpty())
                            "暂无描述"
                        else
                            mData.introduction
                    dialog.show()
                }
//                if (introduce_desc.lineCount > 5) {
                mView.introduceMore.visible()
//                } else {
//                    introduce_more.invisible()
//                }
                if (it.configInfoList != null && it.configInfoList.size > 0) {
                    mView.clConfigure.visible()
                    var list: List<ConfigInfo>
                    if (it.configInfoList.size > 5) {
                        mView.configureMore.visible()
                        list = it.configInfoList.subList(0, 5)
                        var list2 = it.configInfoList
                        mView.configureMore.setOnClickListener {
                            var v =
                                View.inflate(this, R.layout.popwindow_store_configure_more, null)
                            var dialog = getBottomDialog(v, 0.4)
                            v.findViewById<View>(R.id.iv_close)
                                .setOnClickListener { dialog.dismiss() }
                            v.findViewById<View>(R.id.tv_close)
                                .setOnClickListener { dialog.dismiss() }
                            v.findViewById<TextView>(R.id.tv_empty).text = "房型配置"

                            v.findViewById<RecyclerView>(R.id.recyclerView).vertical(5).bindData(
                                list2,
                                R.layout.item_apartment_details_configure
                            ) { holder, item, position ->
                                holder.loadImage(R.id.img, item.iconUrl)
                                holder.setText(R.id.name, item.configName)
                            }
                            dialog.show()
                        }
                    } else {
                        mView.configureMore.gone()
                        list = it.configInfoList!!
                    }
                    //门店配置
                    mView.rvConfigure.vertical(5).bindData(
                        list,
                        R.layout.item_apartment_details_configure
                    ) { holder, item, position ->
                        holder.loadImage(R.id.img, item.iconUrl)
                        holder.setText(R.id.name, item.configName)
                    }
                } else {
                    mView.clConfigure.gone()
                }
            }
        })

        mViewModel.collection_add.observe(this, Observer {
            toast("收藏成功")
        })
        mViewModel.collection_add.state.observe(this, getObserver(mViewModel.collection_add))

        mViewModel.collection_cancel.observe(this, Observer {
            toast("取消收藏")
        })
        mViewModel.collection_cancel.state.observe(this, getObserver(mViewModel.collection_cancel))
        mViewModel.check_sign_online.observe(this, Observer {
            if (!it.isNullOrEmpty()) {
                when (it) {//0成功，跳转到在线签约页面，1未实名认证，跳转到实名认证 2无电子模版
                    "0" -> {
                        Intent().put("houseItemName", houseItemName)
                            .put("roomTypeName", mData.roomTypeName)
                            .put("phone", mData.managerInfo.managerPhone)
                            .put("houseId", mData.id).put(
                                "url",
                                if (mData.picList != null && mData.picList.size > 0) mData.picList[0].url else ""
                            ).start(this, ZKReserveSearchActivity::class.java)
                    }
                    "1" -> {
                        MaterialDialog.Builder(this)
                            .content("未实名认证，请您先进行实名认证")
                            .negativeText("取消")
                            .positiveText("去实名")
                            .positiveColor(color(R.color.color_1273f7))
                            .contentColor(color(R.color.black_66))
                            .negativeColor(color(R.color.gray_99))
                            .cancelable(false)
                            .onNegative { dialog, which ->
                                dialog.dismiss()
                            }
                            .onPositive { dialog, which ->
                                Intent().start(this, ZKIdAuthActivity::class.java)
                                dialog.dismiss()
                            }
                            .show()
                    }
                    "2" -> {
                        MaterialDialog.Builder(this)
                            .content("当前房型未配置电子合同模板\n暂不支持签约！")
                            .positiveText("确定")
                            .positiveColor(color(R.color.color_1273f7))
                            .contentColor(color(R.color.black_66))
                            .cancelable(false)
                            .onPositive { dialog, which ->
                                dialog.dismiss()
                            }
                            .show()
                    }
                    else -> {
                        toast("获取签约状态错误${mViewModel.check_sign_online.errMsg}")
                    }
                }
            } else {
                toast("获取签约状态错误${mViewModel.check_sign_online.errMsg}")
            }
        })
        mViewModel.check_sign_online.state.observe(this, getObserver(mViewModel.check_sign_online))
    }

}